//登录
const btnLogin = document.querySelector('#btn-login')
btnLogin.addEventListener('click', async function () {
    const form = document.querySelector('.login-form')
    const data = serialize(form, { hash: true, empty: true })
    if (data.username.length == 0 || data.password.length == 0) return showToast('用户名或密码不能为空')
    const unameReg = /^\w{8,30}$/
    const pwdReg = /^\w{6,30}$/
    if (!unameReg.test(data.username)) return showToast('用户名应在8-30字符之间')
    if (!pwdReg.test(data.password)) return showToast('密码应在6-30字符之间')
    try {
        const res = await axios({ method: 'post', url: '/login', data })
        // console.log(res);
        setItem('token', res.data.token)
        setItem('username', res.data.username)
        setTimeout(() => {
            location.href = 'index.html'
        }, 1500);
        showToast(res.message)
    } catch (error) {
        showToast(error.response.data.message)
    }
})